﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.BLL;
using Garlic.CC.BaseClass;
using Garlic.CC.WebUtility;
using Garlic.DM.DataModel;
using System.Collections.Generic;
using Garlic.CC.Utility;

namespace Garlic.SystemManagement
{
    public partial class SetMenuToUser : BasePage
    {

        private List<MenuInfo> inRoleMenus;

        #region page load
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //searchParentMenu.Text = hdParent_menu_name.Value;
                //WorkshopMgr = new WorkshopManager(this.CurrentUser);
                if (!Page.IsPostBack)
                {
                    //Init Page
                    //   InitSearchBar();

                    string roleID = Request.QueryString["UserID"];
                    if (string.IsNullOrEmpty(roleID))
                        throw new Exception();

                   // RoleInfo role = RoleManager.GetInfo(int.Parse(roleID));

                   // roleName.InnerText = role.Role_name;
                    hdRoleID.Value = roleID;

                    //string parentMenuID = Request.QueryString["parentMenuID"];
                    //string parentMenuName = Request.QueryString["parentMenuName"];
                    //if (!string.IsNullOrEmpty(parentMenuID))
                    //{
                    //    hdParent_menu_id.Value = parentMenuID;
                    //    hdParent_menu_name.Value = parentMenuName;
                    //    searchParentMenu.Text = parentMenuName;
                    //}

                    //this.Search();
                   // MenuManager.BindMenuList(this.searchParentMenu);
                    //string parentMenuID = Request.QueryString["parentMenuID"];
                    //string parentMenuName = Request.QueryString["parentMenuName"];
                    //if (!string.IsNullOrEmpty(parentMenuID))
                    //{
                    //    this.searchParentMenu.SelectedValue = parentMenuID;
                    //    //this.Search();
                    //}

                    this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = roleID;
                }
            }
            catch (Exception ex)
            {
                //this.HandleException(ex);
            }
        }

        #endregion

        #region method
        /// <summary>
        /// 初始化页面
        /// </summary>
        private void InitPage()
        {

        }

        /// <summary>
        /// 初始化查询栏
        /// </summary>
        private void InitSearchBar()
        {
            //初始化查询列表
            // this.GeneralSearchBar1.ListSourcXYbles = WorkshopMgr.GetSearchBarDataSourcXYbles();


        }

        private void SaveRecords()
        {
            List<int> checkedRecords = new List<int>();
            List<int> allRecords = new List<int>();
            for (int i = 0; i < this.SmartGrid.Rows.Count; i++)
            {
                CheckBox cbSelected = this.SmartGrid.Rows[i].FindControl("checkitem") as CheckBox;
                if (cbSelected != null)
                {
                    if (!cbSelected.Checked)
                        checkedRecords.Add(int.Parse(SmartGrid.DataKeys[i].Value.ToString()));
                    allRecords.Add(int.Parse(SmartGrid.DataKeys[i].Value.ToString()));
                }
            }
            //if (checkedRecords.Count == 0) return;

            int roleID = int.Parse(hdRoleID.Value);

            foreach (int menuID in allRecords)
            {
                MenuManager.AddMenuToUser(menuID, roleID, 1);
            }

            foreach (int menuID in checkedRecords)
            {
                MenuManager.AddMenuToUser(menuID, roleID, 0);
            }

            this.ShowMessage("成功。");
        }

        private void Search()
        {
            //string condition = this.GeneralSearchBar1.GetConditionStr();
            //string condition = "";

            //if (searchMenuName.Text.Trim() != "")
            //    condition += " menu_name like '%" + SqlHelper.ReplaceInvalidWords(searchMenuName.Text) + "%'";

            //if (!string.IsNullOrEmpty(this.searchParentMenu.SelectedValue))
            //{
            //    if (condition != "")
            //        condition += " and ";
            //    condition += " parent_menu_id = " + searchParentMenu.SelectedValue;
            //}


            //this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = condition;
        }

        private void ShowMessage(string msg)
        {
            this.Message = msg;
        }

        #endregion

        #region toolbar event handler
        /// <summary>
        /// 工具栏事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
        {
            switch (args.Command)
            {
                case ToolbarCommand.Save:
                    SaveRecords();
                    break;
                case ToolbarCommand.Search:
                    Search();
                    break;
                //case ToolbarCommand.HideOrExpand:
                //    this.divSearch.Visible = !this.divSearch.Visible;
                //    break;
            }

        }
        #endregion


        protected void SmartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[2].Text == "0")
                        e.Row.Cells[2].Text = "系统";
                    else if (e.Row.Cells[2].Text != "")
                    {
                        MenuInfo menu = MenuManager.GetInfo(int.Parse(e.Row.Cells[2].Text));
                        e.Row.Cells[2].Text = menu.Menu_name;
                        MenuInfo parentMenu = MenuManager.GetInfo(menu.Parent_menu_id);
                        if (parentMenu == null)
                        {
                            parentMenu = new MenuInfo();
                            parentMenu.Menu_id = 0;
                            parentMenu.Menu_name = "系统";
                        }
                        e.Row.Cells[2].Text += " (<a href='SetMenuToRole.aspx?parentMenuID=" + parentMenu.Menu_id.ToString() + "&parentMenuName=" + parentMenu.Menu_name + "&roleID=" + hdRoleID.Value + "'>查看上级菜单</a>)";
                    }
                    //e.Row.Cells[1].Text = searchMenuName.Text;

                    if (inRoleMenus == null)
                    {
                        inRoleMenus = MenuManager.GetMenusByUserID(int.Parse(hdRoleID.Value));
                    }

                    foreach (MenuInfo menu in inRoleMenus)
                    {
                        if (int.Parse(SmartGrid.DataKeys[e.Row.DataItemIndex - SmartGrid.PageSize * (SmartGrid.PageIndex)].Value.ToString()) == menu.Menu_id)
                        {
                            //CheckBox cbSelected = this.SmartGrid.Rows[e.Row.DataItemIndex].FindControl("checkitem") as CheckBox;
                            CheckBox cbSelected = e.Row.FindControl("checkitem") as CheckBox;
                            if (cbSelected != null)
                            {
                                cbSelected.Checked = true;
                            }
                            break;
                        }
                    }
                    HiddenField hf = e.Row.FindControl("hf_iseffect") as HiddenField;
                    CheckBox ckbox = e.Row.FindControl("checkall") as CheckBox;
                    if (hf != null)
                    {
                        if (hf.Value == "1")
                        {
                            if (ckbox != null)
                            {
                                ckbox.Checked = true;
                            }
                        }
                        else if (hf.Value == "0")
                        {
                            if (ckbox != null)
                            {
                                ckbox.Checked = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
               // this.HandleException(ex);
            }
        }
    }//end class
}